#!/bin/sh

update-alternatives --install %OPTWARE_TARGET_PREFIX%/bin/scp scp %OPTWARE_TARGET_PREFIX%/sbin/dropbearmulti 50
update-alternatives --install %OPTWARE_TARGET_PREFIX%/bin/ssh ssh %OPTWARE_TARGET_PREFIX%/sbin/dropbearmulti 50

echo

if [ ! -e /dev/urandom ]; then
    unset REPLY
    until [ "$REPLY" == "y" -o "$REPLY" == "n" ]
    do
        echo -n "/dev/urandom was not found and is required by dropbear.  Create it? (y/n):"
        read REPLY
    done
    if [ "$REPLY" == "y" ]; then
      mknod /dev/urandom c: 1 9
    else
      echo "Dropbear will not operate without /dev/urandom, exiting."
      exit 1
    fi
fi

mkdir -p %OPTWARE_TARGET_PREFIX%/etc/dropbear

if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/dropbear/dropbear_rsa_host_key ]; then
    %OPTWARE_TARGET_PREFIX%/sbin/dropbearkey -t rsa -f %OPTWARE_TARGET_PREFIX%/etc/dropbear/dropbear_rsa_host_key
fi

if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/dropbear/dropbear_dss_host_key ]; then
    %OPTWARE_TARGET_PREFIX%/sbin/dropbearkey -t dss -f %OPTWARE_TARGET_PREFIX%/etc/dropbear/dropbear_dss_host_key
fi

if [ ! -f %OPTWARE_TARGET_PREFIX%/etc/dropbear/dropbear_ecdsa_host_key ]; then
    %OPTWARE_TARGET_PREFIX%/sbin/dropbearkey -t ecdsa -f %OPTWARE_TARGET_PREFIX%/etc/dropbear/dropbear_ecdsa_host_key
fi

if [ -z `pidof dropbear` ]; then
        %OPTWARE_TARGET_PREFIX%/etc/init.d/S51dropbear
fi
